﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class StockRequest : Form
    {
        public StockRequest()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string ActiveUserID { get; set; }
        public DataTable LineItems { get; set; }

        #region Methods

        bool IsAutoNumber()
        {
            try
            {
                string _DOCTYPE = "104";

                SeriesManager oSeries = new SeriesManager();
                oSeries.ConnectionString = this.ConnectionString;
                oSeries.Open();
                bool IsAutoSeries = oSeries.IsAutoNumber(_DOCTYPE);

                oSeries.Close();

                return IsAutoSeries;
            
            }
            catch { return false; }
        }

        void RemoveBlankLastRowFirst()
        {
            try
            {
                int iCount = -1;
                gridView1.MoveLast();

                int[] oRow = gridView1.GetSelectedRows();

                iCount = oRow[0];
                string sItemCode = "";


                sItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemCode"]).ToString();

                if (sItemCode.Trim() == "")
                {
                    gridView1.DeleteRow(iCount);
                }
            }
            catch
            {

            }

        }

        string GetNewSeries()
        {
            string _SERIES = "";
            string _DOCTYPE = "104";

            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();
            if (oSeries.IsAutoNumber(_DOCTYPE) == true)
            {
                _SERIES = oSeries.GetNewSeries(_DOCTYPE);
            }
            else
            {
                _SERIES = txtDocNum.Text;
            }
            oSeries.Close();

            return _SERIES;
        }

        DataTable GetTable()
        {
            DataTable __table = new DataTable();

            __table.Columns.Add("ItemCode", typeof(string));
            __table.Columns.Add("ItemName", typeof(string));
            __table.Columns.Add("Qty", typeof(string));
            __table.Columns.Add("UoM", typeof(string));
            

            return __table;
        }

        bool HasRowItems()
        {
            try
            {
                int iCount = 0;

                iCount = gridView1.RowCount;

                if (iCount > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Control Events
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            StockRequestUnit oUnit = new StockRequestUnit();
            StockRequestManager oManager = new StockRequestManager();
            
            RemoveBlankLastRowFirst();
            
            if (txtCardCode.Text.Trim() == "" || HasRowItems() == false)
            {
                MessageBox.Show("Please insert required information.", "Request", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (gridView1.DataRowCount <= 0)
                {
                    gridView1.AddNewRow();
                }
            }
            else
            {
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                txtDocNum.Text = GetNewSeries();
                oUnit.DocNumber = txtDocNum.Text;
                oUnit.CardCode = txtCardCode.Text;
                oUnit.CardName = txtCardName.Text;
                oUnit.Reference = txtReference.Text;
                oUnit.Remarks = txtRemarks.Text;
                oUnit.DateNeeded = Convert.ToDateTime(dateEdit1.EditValue);

                oUnit.DateCreated = Convert.ToDateTime(txtDateCreated.Text);
                oUnit.CreatedBy = txtCreatedBy.Text;
                oUnit.DateModified = Convert.ToDateTime(txtDateModified.Text);
                oUnit.ModifiedBy = txtModifiedBy.Text;
                oUnit.DocumentStatus = txtStatus.Text;

                oUnit.LineItems = this.LineItems;
                oManager.AddRequest(oUnit);

                oManager.Close();


                #region Disable Controls

                btnAdd.Enabled = false;
                
                txtDocNum.Properties.ReadOnly = true;
                txtCardCode.Properties.ReadOnly = true;
                txtCardName.ReadOnly = true;
                txtReference.ReadOnly = true;
                txtRemarks.ReadOnly = true;
                
                

                #endregion
            }
        }

        private void StockRequest_Load(object sender, EventArgs e)
        {
            System.DateTime oDateValue = System.DateTime.Now;

            txtDateCreated.Text = oDateValue.ToString();
            txtCreatedBy.Text = this.ActiveUserID;
            txtDateModified.Text = oDateValue.ToString();
            txtModifiedBy.Text = this.ActiveUserID;

            this.LineItems = GetTable();
            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();

            if (gridView1.DataRowCount <= 0)
            {
                gridView1.AddNewRow();
            }


            if (IsAutoNumber() == true)
            {
                txtDocNum.Text = "#########";
                txtDocNum.Properties.ReadOnly = true;
            } else
            {
                txtDocNum.Text = "";
                txtDocNum.Properties.ReadOnly = false;
                txtDocNum.Enabled = true;
            }
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                int iCount = -1;
                int[] oRow = gridView1.GetSelectedRows();

                iCount = oRow[0];

                if (e.KeyCode == Keys.Down)
                {
                    if (gridView1.IsLastRow)
                    {
                        string sItemCode = "";
                        oRow = gridView1.GetSelectedRows();

                        iCount = oRow[0];
                        sItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemCode"]).ToString();

                        if (sItemCode != "")
                        {
                            gridView1.AddNewRow();
                        }
                    }
                }
            }
            catch
            {
            }
        }

        private void txtCardCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_BP _SearchBP = new FND_BP();
            
            AddressBookManager oBPManager = new AddressBookManager();
            TaxManager oTxManager = new TaxManager();

            try
            {
                _SearchBP.ConnectionString = this.ConnectionString;
                _SearchBP.BusinessPartnerType = "Customer";
                _SearchBP.ShowDialog();

                txtCardCode.Text = _SearchBP.CardCode;
                txtCardName.Text = _SearchBP.CardName;
               
            }
            catch
            {
            }
        }

        private void txtItemCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
             int iCount = -1;
            int[] oRow = gridView1.GetSelectedRows();

            iCount = oRow[0];

            FND_ITM_STCKRQ _FindItemInSalesQoutForm = new FND_ITM_STCKRQ();

            _FindItemInSalesQoutForm.ConnectionString = this.ConnectionString;
            
            _FindItemInSalesQoutForm.ShowDialog();

            if (_FindItemInSalesQoutForm.SelectedItemCode != null)
            {
                if (_FindItemInSalesQoutForm.SelectedItemCode.Trim() != "")
                {

                    gridView1.SetRowCellValue(iCount, gridView1.Columns["ItemCode"], _FindItemInSalesQoutForm.SelectedItemCode);
                    gridView1.SetRowCellValue(iCount, gridView1.Columns["ItemName"], _FindItemInSalesQoutForm.SelectedItemDescription);
                    gridView1.SetRowCellValue(iCount, gridView1.Columns["UoM"], _FindItemInSalesQoutForm.SelectedUoM);
                    
                }
            }
        }

        private void txtItemCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ItemManager oManager = new ItemManager();

                int iCount = -1;
                int[] oRow = gridView1.GetSelectedRows();

                iCount = oRow[0];
                string sItemCode = "";


                sItemCode = ((DevExpress.XtraEditors.ButtonEdit)sender).EditValue.ToString();

                

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                DataTable table = oManager.GetItemMasterData(sItemCode);

                foreach (DataRow _Row in table.Rows)
                {
                    gridView1.SetRowCellValue(iCount, gridView1.Columns["ItemCode"], _Row["ItemCode"].ToString());
                    gridView1.SetRowCellValue(iCount, gridView1.Columns["ItemName"], _Row["Description"].ToString());
                    gridView1.SetRowCellValue(iCount, gridView1.Columns["UoM"], _Row["InvtyUntMsr"].ToString());

                    break;
                }


                oManager.Close();

            }
        }

        #endregion

        private void txtStatus_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

        }



    }
}
